package com.salts.util;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import java.io.File;
import java.net.URL;
import java.util.Date;
public class readImgUtil {

    private String endpoint;
    private String accessKeyId="TAI5tE4NqTLn9tNig4MN1Ld";
    private String accessKeySecret="cJHT2YqpWiJzfCTYBTB8niBpboZytl";
    private String bucketName="img";
    private String url="clothimg.oss-cn-beijing.aliyuncs.com";

    public readImgUtil(String endpoint, String accessKeyId, String accessKeySecret, String bucketName) {
        this.endpoint = endpoint;
        this.accessKeyId = accessKeyId;
        this.accessKeySecret = accessKeySecret;
        this.bucketName = bucketName;
    }

    public URL uploadImageAndGetURL(String localFilePath, String objectName) {
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 上传文件
        ossClient.putObject(bucketName, objectName, new File(localFilePath));
        // 设置过期时间，生成可访问链接
        Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000); // 设置链接有效期为1小时
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName);
        request.setExpiration(expiration);
        // 生成可访问链接
        URL url = ossClient.generatePresignedUrl(request);
        // 关闭OSSClient
        ossClient.shutdown();
        return url;
    }
}
